package com.lkd.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkd.common.VMSystem;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.feign.VMService;
import com.lkd.service.VendingMachineService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author FJL
 * @Description TODO
 * @Date 2022/8/28 10:52
 **/
public class SupplyJob {

    @Autowired
    private VendingMachineService vendingMachineService;

    @XxlJob("supplyJobHandler")
    public ReturnT<String> supplyJobHandler(){
        ShardingUtil.ShardingVO shardingVo = ShardingUtil.getShardingVo();
        int total = shardingVo.getTotal();
        int index = shardingVo.getIndex();
        XxlJobLogger.log("分片参数  当前分片索引{} 总分片数{}",index,total);


        XxlJobLogger.log("每日自动补货初始化");
        QueryWrapper<VendingMachineEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING)
                .apply("mod(id,"+total+")="+index );// 对id取模
        vendingMachineService.list(qw).forEach(vm-> {
           vendingMachineService.sendTask( vm.getInnerCode());
        });
        return ReturnT.SUCCESS;
    }
}
